home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_elisp-manual-19.idb / usr / freeware / info / elisp-19.z / elisp-19 (.txt)
GNU Info File  |  1998-05-26  |  50KB  |  867 lines

  1. This is Info file elisp, produced by Makeinfo-1.63 from the input file
  2. elisp.texi.
  3.    This version is the edition 2.4.2 of the GNU Emacs Lisp Reference
  4. Manual.  It corresponds to Emacs Version 19.34.
  5.    Published by the Free Software Foundation 59 Temple Place, Suite 330
  6. Boston, MA  02111-1307  USA
  7.    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996 Free Software
  8. Foundation, Inc.
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided that the
  14. entire resulting derived work is distributed under the terms of a
  15. permission notice identical to this one.
  16.    Permission is granted to copy and distribute translations of this
  17. manual into another language, under the above conditions for modified
  18. versions, except that this permission notice may be stated in a
  19. translation approved by the Foundation.
  20.    Permission is granted to copy and distribute modified versions of
  21. this manual under the conditions for verbatim copying, provided also
  22. that the section entitled "GNU General Public License" is included
  23. exactly as in the original, and provided that the entire resulting
  24. derived work is distributed under the terms of a permission notice
  25. identical to this one.
  26.    Permission is granted to copy and distribute translations of this
  27. manual into another language, under the above conditions for modified
  28. versions, except that the section entitled "GNU General Public License"
  29. may be included in a translation approved by the Free Software
  30. Foundation instead of in the original English.
  31. File: elisp,  Node: Hooks,  Prev: Mode Line Format,  Up: Modes
  32. Hooks
  33. =====
  34.    A "hook" is a variable where you can store a function or functions
  35. to be called on a particular occasion by an existing program.  Emacs
  36. provides hooks for the sake of customization.  Most often, hooks are set
  37. up in the `.emacs' file, but Lisp programs can set them also.  *Note
  38. Standard Hooks::, for a list of standard hook variables.
  39.    Most of the hooks in Emacs are "normal hooks".  These variables
  40. contain lists of functions to be called with no arguments.  When the
  41. hook name ends in `-hook', that tells you it is normal.  We try to make
  42. all hooks normal, as much as possible, so that you can use them in a
  43. uniform way.
  44.    Every major mode function is supposed to run a normal hook called the
  45. "mode hook" as the last step of initialization.  This makes it easy for
  46. a user to customize the behavior of the mode, by overriding the local
  47. variable assignments already made by the mode.  But hooks are used in
  48. other contexts too.  For example, the hook `suspend-hook' runs just
  49. before Emacs suspends itself (*note Suspending Emacs::.).
  50.    The recommended way to add a hook function to a normal hook is by
  51. calling `add-hook' (see below).  The hook functions may be any of the
  52. valid kinds of functions that `funcall' accepts (*note What Is a
  53. Function::.).  Most normal hook variables are initially void;
  54. `add-hook' knows how to deal with this.
  55.    If the hook variable's name does not end with `-hook', that
  56. indicates it is probably an abnormal hook; you should look at its
  57. documentation to see how to use the hook properly.
  58.    If the variable's name ends in `-functions' or `-hooks', then the
  59. value is a list of functions, but it is abnormal in that either these
  60. functions are called with arguments or their values are used in some
  61. way.  You can use `add-hook' to add a function to the list, but you
  62. must take care in writing the function.  (A few of these variables are
  63. actually normal hooks which were named before we established the
  64. convention of using `-hook' for them.)
  65.    If the variable's name ends in `-function', then its value is just a
  66. single function, not a list of functions.
  67.    Here's an expression that uses a mode hook to turn on Auto Fill mode
  68. when in Lisp Interaction mode:
  69.      (add-hook 'lisp-interaction-mode-hook 'turn-on-auto-fill)
  70.    The next example shows how to use a hook to customize the way Emacs
  71. formats C code.  (People often have strong personal preferences for one
  72. format or another.)  Here the hook function is an anonymous lambda
  73. expression.
  74.      (add-hook 'c-mode-hook
  75.        (function (lambda ()
  76.                    (setq c-indent-level 4
  77.                          c-argdecl-indent 0
  78.                          c-label-offset -4
  79.                          c-continued-statement-indent 0
  80.                          c-brace-offset 0
  81.                          comment-column 40))))
  82.      
  83.      (setq c++-mode-hook c-mode-hook)
  84.    At the appropriate time, Emacs uses the `run-hooks' function to run
  85. particular hooks.  This function calls the hook functions that have
  86. been added with `add-hook'.
  87.  - Function: run-hooks &rest HOOKVAR
  88.      This function takes one or more hook variable names as arguments,
  89.      and runs each hook in turn.  Each HOOKVAR argument should be a
  90.      symbol that is a hook variable.  These arguments are processed in
  91.      the order specified.
  92.      If a hook variable has a non-`nil' value, that value may be a
  93.      function or a list of functions.  If the value is a function
  94.      (either a lambda expression or a symbol with a function
  95.      definition), it is called.  If it is a list, the elements are
  96.      called, in order.  The hook functions are called with no arguments.
  97.      For example, here's how `emacs-lisp-mode' runs its mode hook:
  98.           (run-hooks 'emacs-lisp-mode-hook)
  99.  - Function: add-hook HOOK FUNCTION &optional APPEND LOCAL
  100.      This function is the handy way to add function FUNCTION to hook
  101.      variable HOOK.  The argument FUNCTION may be any valid Lisp
  102.      function with the proper number of arguments.  For example,
  103.           (add-hook 'text-mode-hook 'my-text-hook-function)
  104.      adds `my-text-hook-function' to the hook called `text-mode-hook'.
  105.      You can use `add-hook' for abnormal hooks as well as for normal
  106.      hooks.
  107.      It is best to design your hook functions so that the order in
  108.      which they are executed does not matter.  Any dependence on the
  109.      order is "asking for trouble."  However, the order is predictable:
  110.      normally, FUNCTION goes at the front of the hook list, so it will
  111.      be executed first (barring another `add-hook' call).
  112.      If the optional argument APPEND is non-`nil', the new hook
  113.      function goes at the end of the hook list and will be executed
  114.      last.
  115.      If LOCAL is non-`nil', that says to make the new hook function
  116.      local to the current buffer.  Before you can do this, you must
  117.      make the hook itself buffer-local by calling `make-local-hook'
  118.      (*not* `make-local-variable').  If the hook itself is not
  119.      buffer-local, then the value of LOCAL makes no difference--the
  120.      hook function is always global.
  121.  - Function: remove-hook HOOK FUNCTION &optional LOCAL
  122.      This function removes FUNCTION from the hook variable HOOK.
  123.      If LOCAL is non-`nil', that says to remove FUNCTION from the local
  124.      hook list instead of from the global hook list.  If the hook
  125.      itself is not buffer-local, then the value of LOCAL makes no
  126.      difference.
  127.  - Function: make-local-hook HOOK
  128.      This function makes the hook variable `hook' local to the current
  129.      buffer.  When a hook variable is local, it can have local and
  130.      global hook functions, and `run-hooks' runs all of them.
  131.      This function works by making `t' an element of the buffer-local
  132.      value.  That serves as a flag to use the hook functions in the
  133.      default value of the hook variable as well as those in the local
  134.      value.  Since `run-hooks' understands this flag, `make-local-hook'
  135.      works with all normal hooks.  It works for only some non-normal
  136.      hooks--those whose callers have been updated to understand this
  137.      meaning of `t'.
  138.      Do not use `make-local-variable' directly for hook variables; it is
  139.      not sufficient.
  140. File: elisp,  Node: Documentation,  Next: Files,  Prev: Modes,  Up: Top
  141. Documentation
  142. *************
  143.    GNU Emacs Lisp has convenient on-line help facilities, most of which
  144. derive their information from the documentation strings associated with
  145. functions and variables.  This chapter describes how to write good
  146. documentation strings for your Lisp programs, as well as how to write
  147. programs to access documentation.
  148.    Note that the documentation strings for Emacs are not the same thing
  149. as the Emacs manual.  Manuals have their own source files, written in
  150. the Texinfo language; documentation strings are specified in the
  151. definitions of the functions and variables they apply to.  A collection
  152. of documentation strings is not sufficient as a manual because a good
  153. manual is not organized in that fashion; it is organized in terms of
  154. topics of discussion.
  155. * Menu:
  156. * Documentation Basics::      Good style for doc strings.
  157.                                 Where to put them.  How Emacs stores them.
  158. * Accessing Documentation::   How Lisp programs can access doc strings.
  159. * Keys in Documentation::     Substituting current key bindings.
  160. * Describing Characters::     Making printable descriptions of
  161.                                 non-printing characters and key sequences.
  162. * Help Functions::            Subroutines used by Emacs help facilities.
  163. File: elisp,  Node: Documentation Basics,  Next: Accessing Documentation,  Up: Documentation
  164. Documentation Basics
  165. ====================
  166.    A documentation string is written using the Lisp syntax for strings,
  167. with double-quote characters surrounding the text of the string.  This
  168. is because it really is a Lisp string object.  The string serves as
  169. documentation when it is written in the proper place in the definition
  170. of a function or variable.  In a function definition, the documentation
  171. string follows the argument list.  In a variable definition, the
  172. documentation string follows the initial value of the variable.
  173.    When you write a documentation string, make the first line a complete
  174. sentence (or two complete sentences) since some commands, such as
  175. `apropos', show only the first line of a multi-line documentation
  176. string.  Also, you should not indent the second line of a documentation
  177. string, if you have one, because that looks odd when you use `C-h f'
  178. (`describe-function') or `C-h v' (`describe-variable').  *Note
  179. Documentation Tips::.
  180.    Documentation strings may contain several special substrings, which
  181. stand for key bindings to be looked up in the current keymaps when the
  182. documentation is displayed.  This allows documentation strings to refer
  183. to the keys for related commands and be accurate even when a user
  184. rearranges the key bindings.  (*Note Accessing Documentation::.)
  185.    Within the Lisp world, a documentation string accessible through the
  186. function or variable that it describes:
  187.    * The documentation for a function is stored in the function
  188.      definition itself (*note Lambda Expressions::.).  The function
  189.      `documentation' knows how to extract it.
  190.    * The documentation for a variable is stored in the variable's
  191.      property list under the property name `variable-documentation'.
  192.      The function `documentation-property' knows how to extract it.
  193.    To save space, the documentation for preloaded functions and
  194. variables (including primitive functions and autoloaded functions) is
  195. stored in the file `emacs/etc/DOC-VERSION'.  The documentation for
  196. functions and variables loaded during the Emacs session from
  197. byte-compiled files is stored in those files (*note Docs and
  198. Compilation::.).
  199.    The data structure inside Emacs has an integer offset into the file,
  200. or a list containing a string and an integer, in place of the
  201. documentation string.  The functions `documentation' and
  202. `documentation-property' use that information to read the documentation
  203. from the appropriate file; this is transparent to the user.
  204.    For information on the uses of documentation strings, see *Note
  205. Help: (emacs)Help.
  206.    The `emacs/lib-src' directory contains two utilities that you can
  207. use to print nice-looking hardcopy for the file
  208. `emacs/etc/DOC-VERSION'.  These are `sorted-doc.c' and `digest-doc.c'.
  209. File: elisp,  Node: Accessing Documentation,  Next: Keys in Documentation,  Prev: Documentation Basics,  Up: Documentation
  210. Access to Documentation Strings
  211. ===============================
  212.  - Function: documentation-property SYMBOL PROPERTY &optional VERBATIM
  213.      This function returns the documentation string that is recorded
  214.      SYMBOL's property list under property PROPERTY.  It retrieves the
  215.      text from a file if necessary, and runs `substitute-command-keys'
  216.      to substitute actual key bindings.  (This substitution is not done
  217.      if VERBATIM is non-`nil'; the VERBATIM argument exists only as of
  218.      Emacs 19.)
  219.           (documentation-property 'command-line-processed
  220.              'variable-documentation)
  221.                => "t once command line has been processed"
  222.           (symbol-plist 'command-line-processed)
  223.                => (variable-documentation 188902)
  224.  - Function: documentation FUNCTION &optional VERBATIM
  225.      This function returns the documentation string of FUNCTION.  It
  226.      reads the text from a file if necessary.  Then (unless VERBATIM is
  227.      non-`nil') it calls `substitute-command-keys', to return a value
  228.      containing the actual (current) key bindings.
  229.      The function `documentation' signals a `void-function' error if
  230.      FUNCTION has no function definition.  However, it is ok if the
  231.      function definition has no documentation string.  In that case,
  232.      `documentation' returns `nil'.
  233.    Here is an example of using the two functions, `documentation' and
  234. `documentation-property', to display the documentation strings for
  235. several symbols in a `*Help*' buffer.
  236.      (defun describe-symbols (pattern)
  237.        "Describe the Emacs Lisp symbols matching PATTERN.
  238.      All symbols that have PATTERN in their name are described
  239.      in the `*Help*' buffer."
  240.        (interactive "sDescribe symbols matching: ")
  241.        (let ((describe-func
  242.               (function
  243.                (lambda (s)
  244.      ;; Print description of symbol.
  245.                  (if (fboundp s)             ; It is a function.
  246.                      (princ
  247.                       (format "%s\t%s\n%s\n\n" s
  248.                         (if (commandp s)
  249.                             (let ((keys (where-is-internal s)))
  250.                               (if keys
  251.                                   (concat
  252.                                    "Keys: "
  253.                                    (mapconcat 'key-description
  254.                                               keys " "))
  255.                                 "Keys: none"))
  256.                           "Function")
  257.      (or (documentation s)
  258.                             "not documented"))))
  259.      
  260.                  (if (boundp s)              ; It is a variable.
  261.      (princ
  262.                       (format "%s\t%s\n%s\n\n" s
  263.                         (if (user-variable-p s)
  264.                             "Option " "Variable")
  265.      (or (documentation-property
  266.                               s 'variable-documentation)
  267.                             "not documented")))))))
  268.              sym-list)
  269.      ;; Build a list of symbols that match pattern.
  270.          (mapatoms (function
  271.                     (lambda (sym)
  272.                       (if (string-match pattern (symbol-name sym))
  273.                           (setq sym-list (cons sym sym-list))))))
  274.      ;; Display the data.
  275.          (with-output-to-temp-buffer "*Help*"
  276.            (mapcar describe-func (sort sym-list 'string<))
  277.            (print-help-return-message))))
  278.    The `describe-symbols' function works like `apropos', but provides
  279. more information.
  280.      (describe-symbols "goal")
  281.      
  282.      ---------- Buffer: *Help* ----------
  283.      goal-column     Option
  284.      *Semipermanent goal column for vertical motion, as set by ...
  285.      set-goal-column Command: C-x C-n
  286.      Set the current horizontal position as a goal for C-n and C-p.
  287.      Those commands will move to this position in the line moved to
  288.      rather than trying to keep the same horizontal position.
  289.      With a non-nil argument, clears out the goal column
  290.      so that C-n and C-p resume vertical motion.
  291.      The goal column is stored in the variable `goal-column'.
  292.      temporary-goal-column   Variable
  293.      Current goal column for vertical motion.
  294.      It is the column where point was
  295.      at the start of current run of vertical motion commands.
  296.      When the `track-eol' feature is doing its job, the value is 9999.
  297.      ---------- Buffer: *Help* ----------
  298.  - Function: Snarf-documentation FILENAME
  299.      This function is used only during Emacs initialization, just before
  300.      the runnable Emacs is dumped.  It finds the file offsets of the
  301.      documentation strings stored in the file FILENAME, and records
  302.      them in the in-core function definitions and variable property
  303.      lists in place of the actual strings.  *Note Building Emacs::.
  304.      Emacs finds the file FILENAME in the `emacs/etc' directory.  When
  305.      the dumped Emacs is later executed, the same file is found in the
  306.      directory `doc-directory'.  Usually FILENAME is `"DOC-VERSION"'.
  307.  - Variable: doc-directory
  308.      This variable holds the name of the directory which should contion
  309.      the file `"DOC-VERSION"' that contains documentation strings for
  310.      built-in and preloaded functions and variables.
  311.      In most cases, this is the same as `data-directory'.  They may be
  312.      different when you run Emacs from the directory where you built it,
  313.      without actually installing it.  See `data-directory' in *Note
  314.      Help Functions::.
  315.      In older Emacs versions, `exec-directory' was used for this.
  316. File: elisp,  Node: Keys in Documentation,  Next: Describing Characters,  Prev: Accessing Documentation,  Up: Documentation
  317. Substituting Key Bindings in Documentation
  318. ==========================================
  319.    When documentation strings refer to key sequences, they should use
  320. the current, actual key bindings.  They can do so using certain special
  321. text sequences described below.  Accessing documentation strings in the
  322. usual way substitutes current key binding information for these special
  323. sequences.  This works by calling `substitute-command-keys'.  You can
  324. also call that function yourself.
  325.    Here is a list of the special sequences and what they mean:
  326. `\[COMMAND]'
  327.      stands for a key sequence that will invoke COMMAND, or `M-x
  328.      COMMAND' if COMMAND has no key bindings.
  329. `\{MAPVAR}'
  330.      stands for a summary of the value of MAPVAR, which should be a
  331.      keymap.  The summary is made by `describe-bindings'.
  332. `\<MAPVAR>'
  333.      stands for no text itself.  It is used for a side effect: it
  334.      specifies MAPVAR as the keymap for any following `\[COMMAND]'
  335.      sequences in this documentation string.
  336.      quotes the following character and is discarded; thus, `\=\[' puts
  337.      `\[' into the output, and `\=\=' puts `\=' into the output.
  338.    *Please note:* Each `\' must be doubled when written in a string in
  339. Emacs Lisp.
  340.  - Function: substitute-command-keys STRING
  341.      This function scans STRING for the above special sequences and
  342.      replaces them by what they stand for, returning the result as a
  343.      string.  This permits display of documentation that refers
  344.      accurately to the user's own customized key bindings.
  345.    Here are examples of the special sequences:
  346.      (substitute-command-keys
  347.         "To abort recursive edit, type: \\[abort-recursive-edit]")
  348.      => "To abort recursive edit, type: C-]"
  349.      (substitute-command-keys
  350.         "The keys that are defined for the minibuffer here are:
  351.        \\{minibuffer-local-must-match-map}")
  352.      => "The keys that are defined for the minibuffer here are:
  353.      
  354.      ?               minibuffer-completion-help
  355.      SPC             minibuffer-complete-word
  356.      TAB             minibuffer-complete
  357.      LFD             minibuffer-complete-and-exit
  358.      RET             minibuffer-complete-and-exit
  359.      C-g             abort-recursive-edit
  360.      "
  361.      (substitute-command-keys
  362.         "To abort a recursive edit from the minibuffer, type\
  363.      \\<minibuffer-local-must-match-map>\\[abort-recursive-edit].")
  364.      => "To abort a recursive edit from the minibuffer, type C-g."
  365. File: elisp,  Node: Describing Characters,  Next: Help Functions,  Prev: Keys in Documentation,  Up: Documentation
  366. Describing Characters for Help Messages
  367. =======================================
  368.    These functions convert events, key sequences or characters to
  369. textual descriptions.  These descriptions are useful for including
  370. arbitrary text characters or key sequences in messages, because they
  371. convert non-printing and whitespace characters to sequences of printing
  372. characters.  The description of a non-whitespace printing character is
  373. the character itself.
  374.  - Function: key-description SEQUENCE
  375.      This function returns a string containing the Emacs standard
  376.      notation for the input events in SEQUENCE.  The argument SEQUENCE
  377.      may be a string, vector or list.  *Note Input Events::, for more
  378.      information about valid events.  See also the examples for
  379.      `single-key-description', below.
  380.  - Function: single-key-description EVENT
  381.      This function returns a string describing EVENT in the standard
  382.      Emacs notation for keyboard input.  A normal printing character
  383.      appears as itself, but a control character turns into a string
  384.      starting with `C-', a meta character turns into a string starting
  385.      with `M-', and space, linefeed, etc. appear as `SPC', `LFD', etc.
  386.      A function key symbol appears as itself.  An event that is a list
  387.      appears as the name of the symbol in the CAR of the list.
  388.           (single-key-description ?\C-x)
  389.                => "C-x"
  390.           (key-description "\C-x \M-y \n \t \r \f123")
  391.                => "C-x SPC M-y SPC LFD SPC TAB SPC RET SPC C-l 1 2 3"
  392.           (single-key-description 'C-mouse-1)
  393.                => "C-mouse-1"
  394.  - Function: text-char-description CHARACTER
  395.      This function returns a string describing CHARACTER in the
  396.      standard Emacs notation for characters that appear in text--like
  397.      `single-key-description', except that control characters are
  398.      represented with a leading caret (which is how control characters
  399.      in Emacs buffers are usually displayed).
  400.           (text-char-description ?\C-c)
  401.                => "^C"
  402.           (text-char-description ?\M-m)
  403.                => "M-m"
  404.           (text-char-description ?\C-\M-m)
  405.                => "M-^M"
  406. File: elisp,  Node: Help Functions,  Prev: Describing Characters,  Up: Documentation
  407. Help Functions
  408. ==============
  409.    Emacs provides a variety of on-line help functions, all accessible to
  410. the user as subcommands of the prefix `C-h'.  For more information
  411. about them, see *Note Help: (emacs)Help.  Here we describe some
  412. program-level interfaces to the same information.
  413.  - Command: apropos REGEXP &optional DO-ALL PREDICATE
  414.      This function finds all symbols whose names contain a match for the
  415.      regular expression REGEXP, and returns a list of them (*note
  416.      Regular Expressions::.).  It also displays the symbols in a buffer
  417.      named `*Help*', each with a one-line description.
  418.      If DO-ALL is non-`nil', then `apropos' also shows key bindings for
  419.      the functions that are found.
  420.      If PREDICATE is non-`nil', it should be a function to be called on
  421.      each symbol that has matched REGEXP.  Only symbols for which
  422.      PREDICATE returns a non-`nil' value are listed or displayed.
  423.      In the first of the following examples, `apropos' finds all the
  424.      symbols with names containing `exec'.  In the second example, it
  425.      finds and returns only those symbols that are also commands.  (We
  426.      don't show the output that results in the `*Help*' buffer.)
  427.           (apropos "exec")
  428.                => (Buffer-menu-execute command-execute exec-directory
  429.               exec-path execute-extended-command execute-kbd-macro
  430.               executing-kbd-macro executing-macro)
  431.           (apropos "exec" nil 'commandp)
  432.                => (Buffer-menu-execute execute-extended-command)
  433.      The command `C-h a' (`command-apropos') calls `apropos', but
  434.      specifies a PREDICATE to restrict the output to symbols that are
  435.      commands.  The call to `apropos' looks like this:
  436.           (apropos string t 'commandp)
  437.  - Command: super-apropos REGEXP &optional DO-ALL
  438.      This function differs from `apropos' in that it searches
  439.      documentation strings as well as symbol names for matches for
  440.      REGEXP.  By default, it searches the documentation strings only
  441.      for preloaded functions and variables.  If DO-ALL is non-`nil', it
  442.      scans the names and documentation strings of all functions and
  443.      variables.
  444.  - Variable: help-map
  445.      The value of this variable is a local keymap for characters
  446.      following the Help key, `C-h'.
  447.  - Prefix Command: help-command
  448.      This symbol is not a function; its function definition is actually
  449.      the keymap known as `help-map'.  It is defined in `help.el' as
  450.      follows:
  451.           (define-key global-map "\C-h" 'help-command)
  452.           (fset 'help-command help-map)
  453.  - Function: print-help-return-message &optional FUNCTION
  454.      This function builds a string that explains how to restore the
  455.      previous state of the windows after a help command.  After
  456.      building the message, it applies FUNCTION to it if FUNCTION is
  457.      non-`nil'.  Otherwise it calls `message' to display it in the echo
  458.      area.
  459.      This function expects to be called inside a
  460.      `with-output-to-temp-buffer' special form, and expects
  461.      `standard-output' to have the value bound by that special form.
  462.      For an example of its use, see the long example in *Note Accessing
  463.      Documentation::.
  464.  - Variable: help-char
  465.      The value of this variable is the help character--the character
  466.      that Emacs recognizes as meaning Help.  By default, it is 8, which
  467.      is `C-h'.  When Emacs reads this character, if `help-form' is
  468.      non-`nil' Lisp expression, it evaluates that expression, and
  469.      displays the result in a window if it is a string.
  470.      Usually the value of `help-form''s value is `nil'.  Then the help
  471.      character has no special meaning at the level of command input, and
  472.      it becomes part of a key sequence in the normal way.  The standard
  473.      key binding of `C-h' is a prefix key for several general-purpose
  474.      help features.
  475.      The help character is special after prefix keys, too.  If it has no
  476.      binding as a subcommand of the prefix key, it runs
  477.      `describe-prefix-bindings', which displays a list of all the
  478.      subcommands of the prefix key.
  479.  - Variable: help-form
  480.      If this variable is non-`nil', its value is a form to evaluate
  481.      whenever the character `help-char' is read.  If evaluating the form
  482.      produces a string, that string is displayed.
  483.      A command that calls `read-event' or `read-char' probably should
  484.      bind `help-form' to a non-`nil' expression while it does input.
  485.      (The exception is when `C-h' is meaningful input.) Evaluating this
  486.      expression should result in a string that explains what the input
  487.      is for and how to enter it properly.
  488.      Entry to the minibuffer binds this variable to the value of
  489.      `minibuffer-help-form' (*note Minibuffer Misc::.).
  490.  - Variable: prefix-help-command
  491.      This variable holds a function to print help for a prefix
  492.      character.  The function is called when the user types a prefix
  493.      key followed by the help character, and the help character has no
  494.      binding after that prefix.  The variable's default value is
  495.      `describe-prefix-bindings'.
  496.  - Function: describe-prefix-bindings
  497.      This function calls `describe-bindings' to display a list of all
  498.      the subcommands of the prefix key of the most recent key sequence.
  499.      The prefix described consists of all but the last event of that
  500.      key sequence.  (The last event is, presumably, the help character.)
  501.    The following two functions are found in the library `helper'.  They
  502. are for modes that want to provide help without relinquishing control,
  503. such as the "electric" modes.  You must load that library with
  504. `(require 'helper)' in order to use them.  Their names begin with
  505. `Helper' to distinguish them from the ordinary help functions.
  506.  - Command: Helper-describe-bindings
  507.      This command pops up a window displaying a help buffer containing a
  508.      listing of all of the key bindings from both the local and global
  509.      keymaps.  It works by calling `describe-bindings'.
  510.  - Command: Helper-help
  511.      This command provides help for the current mode.  It prompts the
  512.      user in the minibuffer with the message `Help (Type ? for further
  513.      options)', and then provides assistance in finding out what the key
  514.      bindings are, and what the mode is intended for.  It returns `nil'.
  515.      This can be customized by changing the map `Helper-help-map'.
  516.  - Variable: data-directory
  517.      This variable holds the name of the directory in which Emacs finds
  518.      certain documentation and text files that come with Emacs.  In
  519.      older Emacs versions, `exec-directory' was used for this.
  520.  - Macro: make-help-screen FNAME HELP-LINE HELP-TEXT HELP-MAP
  521.      This macro defines a help command named FNAME that acts like a
  522.      prefix key that shows a list of the subcommands it offers.
  523.      When invoked, FNAME displays HELP-TEXT in a window, then reads and
  524.      executes a key sequence according to HELP-MAP.  The string
  525.      HELP-TEXT should describe the bindings available in HELP-MAP.
  526.      The command FNAME is defined to handle a few events itself, by
  527.      scrolling the display of HELP-TEXT.  When FNAME reads one of those
  528.      special events, it does the scrolling and then reads another
  529.      event.  When it reads an event that is not one of those few, and
  530.      which has a binding in HELP-MAP, it executes that key's binding and
  531.      then returns.
  532.      The argument HELP-LINE should be a single-line summary of the
  533.      alternatives in HELP-MAP.  In the current version of Emacs, this
  534.      argument is used only if you set the option `three-step-help' to
  535.      `t'.
  536.  - User Option: three-step-help
  537.      If this variable is non-`nil', commands defined with
  538.      `make-help-screen' display their HELP-LINE strings in the echo
  539.      area at first, and display the longer HELP-TEXT strings only if
  540.      the user types the help character again.
  541. File: elisp,  Node: Files,  Next: Backups and Auto-Saving,  Prev: Documentation,  Up: Top
  542. Files
  543. *****
  544.    In Emacs, you can find, create, view, save, and otherwise work with
  545. files and file directories.  This chapter describes most of the
  546. file-related functions of Emacs Lisp, but a few others are described in
  547. *Note Buffers::, and those related to backups and auto-saving are
  548. described in *Note Backups and Auto-Saving::.
  549.    Many of the file functions take one or more arguments that are file
  550. names.  A file name is actually a string.  Most of these functions
  551. expand file name arguments using `expand-file-name', so that `~' is
  552. handled correctly, as are relative file names (including `../').  These
  553. functions don't recognize environment variable substitutions such as
  554. `$HOME'.  *Note File Name Expansion::.
  555. * Menu:
  556. * Visiting Files::           Reading files into Emacs buffers for editing.
  557. * Saving Buffers::           Writing changed buffers back into files.
  558. * Reading from Files::       Reading files into buffers without visiting.
  559. * Writing to Files::         Writing new files from parts of buffers.
  560. * File Locks::               Locking and unlocking files, to prevent
  561.                                simultaneous editing by two people.
  562. * Information about Files::  Testing existence, accessibility, size of files.
  563. * Changing File Attributes:: Renaming files, changing protection, etc.
  564. * File Names::               Decomposing and expanding file names.
  565. * Contents of Directories::  Getting a list of the files in a directory.
  566. * Create/Delete Dirs::         Creating and Deleting Directories.
  567. * Magic File Names::         Defining "magic" special handling
  568.                    for certain file names.
  569. * Format Conversion::        Conversion to and from various file formats.
  570. * Files and MS-DOS::         Distinguishing text and binary files on MS-DOS.
  571. File: elisp,  Node: Visiting Files,  Next: Saving Buffers,  Up: Files
  572. Visiting Files
  573. ==============
  574.    Visiting a file means reading a file into a buffer.  Once this is
  575. done, we say that the buffer is "visiting" that file, and call the file
  576. "the visited file" of the buffer.
  577.    A file and a buffer are two different things.  A file is information
  578. recorded permanently in the computer (unless you delete it).  A buffer,
  579. on the other hand, is information inside of Emacs that will vanish at
  580. the end of the editing session (or when you kill the buffer).  Usually,
  581. a buffer contains information that you have copied from a file; then we
  582. say the buffer is visiting that file.  The copy in the buffer is what
  583. you modify with editing commands.  Such changes to the buffer do not
  584. change the file; therefore, to make the changes permanent, you must
  585. "save" the buffer, which means copying the altered buffer contents back
  586. into the file.
  587.    In spite of the distinction between files and buffers, people often
  588. refer to a file when they mean a buffer and vice-versa.  Indeed, we say,
  589. "I am editing a file," rather than, "I am editing a buffer that I will
  590. soon save as a file of the same name."  Humans do not usually need to
  591. make the distinction explicit.  When dealing with a computer program,
  592. however, it is good to keep the distinction in mind.
  593. * Menu:
  594. * Visiting Functions::         The usual interface functions for visiting.
  595. * Subroutines of Visiting::    Lower-level subroutines that they use.
  596. File: elisp,  Node: Visiting Functions,  Next: Subroutines of Visiting,  Up: Visiting Files
  597. Functions for Visiting Files
  598. ----------------------------
  599.    This section describes the functions normally used to visit files.
  600. For historical reasons, these functions have names starting with
  601. `find-' rather than `visit-'.  *Note Buffer File Name::, for functions
  602. and variables that access the visited file name of a buffer or that
  603. find an existing buffer by its visited file name.
  604.    In a Lisp program, if you want to look at the contents of a file but
  605. not alter it, the fastest way is to use `insert-file-contents' in a
  606. temporary buffer.  Visiting the file is not necessary and takes longer.
  607. *Note Reading from Files::.
  608.  - Command: find-file FILENAME
  609.      This command selects a buffer visiting the file FILENAME, using an
  610.      existing buffer if there is one, and otherwise creating a new
  611.      buffer and reading the file into it.  It also returns that buffer.
  612.      The body of the `find-file' function is very simple and looks like
  613.      this:
  614.           (switch-to-buffer (find-file-noselect filename))
  615.      (See `switch-to-buffer' in *Note Displaying Buffers::.)
  616.      When `find-file' is called interactively, it prompts for FILENAME
  617.      in the minibuffer.
  618.  - Function: find-file-noselect FILENAME
  619.      This function is the guts of all the file-visiting functions.  It
  620.      finds or creates a buffer visiting the file FILENAME, and returns
  621.      it.  It uses an existing buffer if there is one, and otherwise
  622.      creates a new buffer and reads the file into it.  You may make the
  623.      buffer current or display it in a window if you wish, but this
  624.      function does not do so.
  625.      When `find-file-noselect' uses an existing buffer, it first
  626.      verifies that the file has not changed since it was last visited or
  627.      saved in that buffer.  If the file has changed, then this function
  628.      asks the user whether to reread the changed file.  If the user says
  629.      `yes', any changes previously made in the buffer are lost.
  630.      If `find-file-noselect' needs to create a buffer, and there is no
  631.      file named FILENAME, it displays the message `New file' in the
  632.      echo area, and leaves the buffer empty.
  633.      The `find-file-noselect' function calls `after-find-file' after
  634.      reading the file (*note Subroutines of Visiting::.).  That function
  635.      sets the buffer major mode, parses local variables, warns the user
  636.      if there exists an auto-save file more recent than the file just
  637.      visited, and finishes by running the functions in
  638.      `find-file-hooks'.
  639.      The `find-file-noselect' function returns the buffer that is
  640.      visiting the file FILENAME.
  641.           (find-file-noselect "/etc/fstab")
  642.                => #<buffer fstab>
  643.  - Command: find-file-other-window FILENAME
  644.      This command selects a buffer visiting the file FILENAME, but does
  645.      so in a window other than the selected window.  It may use another
  646.      existing window or split a window; see *Note Displaying Buffers::.
  647.      When this command is called interactively, it prompts for FILENAME.
  648.  - Command: find-file-read-only FILENAME
  649.      This command selects a buffer visiting the file FILENAME, like
  650.      `find-file', but it marks the buffer as read-only.  *Note Read
  651.      Only Buffers::, for related functions and variables.
  652.      When this command is called interactively, it prompts for FILENAME.
  653.  - Command: view-file FILENAME
  654.      This command visits FILENAME in View mode, and displays it in a
  655.      recursive edit, returning to the previous buffer when done.  View
  656.      mode is a mode that allows you to skim rapidly through the file
  657.      but does not let you modify it.  Entering View mode runs the
  658.      normal hook `view-mode-hook'.  *Note Hooks::.
  659.      When `view-file' is called interactively, it prompts for FILENAME.
  660.  - Variable: find-file-hooks
  661.      The value of this variable is a list of functions to be called
  662.      after a file is visited.  The file's local-variables specification
  663.      (if any) will have been processed before the hooks are run.  The
  664.      buffer visiting the file is current when the hook functions are
  665.      run.
  666.      This variable works just like a normal hook, but we think that
  667.      renaming it would not be advisable.
  668.  - Variable: find-file-not-found-hooks
  669.      The value of this variable is a list of functions to be called when
  670.      `find-file' or `find-file-noselect' is passed a nonexistent file
  671.      name.  `find-file-noselect' calls these functions as soon as it
  672.      detects a nonexistent file.  It calls them in the order of the
  673.      list, until one of them returns non-`nil'.  `buffer-file-name' is
  674.      already set up.
  675.      This is not a normal hook because the values of the functions are
  676.      used and they may not all be called.
  677. File: elisp,  Node: Subroutines of Visiting,  Prev: Visiting Functions,  Up: Visiting Files
  678. Subroutines of Visiting
  679. -----------------------
  680.    The `find-file-noselect' function uses the `create-file-buffer' and
  681. `after-find-file' functions as subroutines.  Sometimes it is useful to
  682. call them directly.
  683.  - Function: create-file-buffer FILENAME
  684.      This function creates a suitably named buffer for visiting
  685.      FILENAME, and returns it.  It uses FILENAME (sans directory) as
  686.      the name if that name is free; otherwise, it appends a string such
  687.      as `<2>' to get an unused name.  See also *Note Creating Buffers::.
  688.      *Please note:* `create-file-buffer' does *not* associate the new
  689.      buffer with a file and does not select the buffer.  It also does
  690.      not use the default major mode.
  691.           (create-file-buffer "foo")
  692.                => #<buffer foo>
  693.           (create-file-buffer "foo")
  694.                => #<buffer foo<2>>
  695.           (create-file-buffer "foo")
  696.                => #<buffer foo<3>>
  697.      This function is used by `find-file-noselect'.  It uses
  698.      `generate-new-buffer' (*note Creating Buffers::.).
  699.  - Function: after-find-file &optional ERROR WARN
  700.      This function sets the buffer major mode, and parses local
  701.      variables (*note Auto Major Mode::.).  It is called by
  702.      `find-file-noselect' and by the default revert function (*note
  703.      Reverting::.).
  704.      If reading the file got an error because the file does not exist,
  705.      but its directory does exist, the caller should pass a non-`nil'
  706.      value for ERROR.  In that case, `after-find-file' issues a warning:
  707.      `(New File)'.  For more serious errors, the caller should usually
  708.      not call `after-find-file'.
  709.      If WARN is non-`nil', then this function issues a warning if an
  710.      auto-save file exists and is more recent than the visited file.
  711.      The last thing `after-find-file' does is call all the functions in
  712.      `find-file-hooks'.
  713. File: elisp,  Node: Saving Buffers,  Next: Reading from Files,  Prev: Visiting Files,  Up: Files
  714. Saving Buffers
  715. ==============
  716.    When you edit a file in Emacs, you are actually working on a buffer
  717. that is visiting that file--that is, the contents of the file are
  718. copied into the buffer and the copy is what you edit.  Changes to the
  719. buffer do not change the file until you "save" the buffer, which means
  720. copying the contents of the buffer into the file.
  721.  - Command: save-buffer &optional BACKUP-OPTION
  722.      This function saves the contents of the current buffer in its
  723.      visited file if the buffer has been modified since it was last
  724.      visited or saved.  Otherwise it does nothing.
  725.      `save-buffer' is responsible for making backup files.  Normally,
  726.      BACKUP-OPTION is `nil', and `save-buffer' makes a backup file only
  727.      if this is the first save since visiting the file.  Other values
  728.      for BACKUP-OPTION request the making of backup files in other
  729.      circumstances:
  730.         * With an argument of 4 or 64, reflecting 1 or 3 `C-u''s, the
  731.           `save-buffer' function marks this version of the file to be
  732.           backed up when the buffer is next saved.
  733.         * With an argument of 16 or 64, reflecting 2 or 3 `C-u''s, the
  734.           `save-buffer' function unconditionally backs up the previous
  735.           version of the file before saving it.
  736.  - Command: save-some-buffers &optional SAVE-SILENTLY-P EXITING
  737.      This command saves some modified file-visiting buffers.  Normally
  738.      it asks the user about each buffer.  But if SAVE-SILENTLY-P is
  739.      non-`nil', it saves all the file-visiting buffers without querying
  740.      the user.
  741.      The optional EXITING argument, if non-`nil', requests this
  742.      function to offer also to save certain other buffers that are not
  743.      visiting files.  These are buffers that have a non-`nil' local
  744.      value of `buffer-offer-save'.  (A user who says yes to saving one
  745.      of these is asked to specify a file name to use.)  The
  746.      `save-buffers-kill-emacs' function passes a non-`nil' value for
  747.      this argument.
  748.  - Variable: buffer-offer-save
  749.      When this variable is non-`nil' in a buffer, Emacs offers to save
  750.      the buffer on exit even if the buffer is not visiting a file.  The
  751.      variable is automatically local in all buffers.  Normally, Mail
  752.      mode (used for editing outgoing mail) sets this to `t'.
  753.  - Command: write-file FILENAME
  754.      This function writes the current buffer into file FILENAME, makes
  755.      the buffer visit that file, and marks it not modified.  Then it
  756.      renames the buffer based on FILENAME, appending a string like `<2>'
  757.      if necessary to make a unique buffer name.  It does most of this
  758.      work by calling `set-visited-file-name' and `save-buffer'.
  759.    Saving a buffer runs several hooks.  It also performs format
  760. conversion (*note Format Conversion::.), and may save text properties in
  761. "annotations" (*note Saving Properties::.).
  762.  - Variable: write-file-hooks
  763.      The value of this variable is a list of functions to be called
  764.      before writing out a buffer to its visited file.  If one of them
  765.      returns non-`nil', the file is considered already written and the
  766.      rest of the functions are not called, nor is the usual code for
  767.      writing the file executed.
  768.      If a function in `write-file-hooks' returns non-`nil', it is
  769.      responsible for making a backup file (if that is appropriate).  To
  770.      do so, execute the following code:
  771.           (or buffer-backed-up (backup-buffer))
  772.      You might wish to save the file modes value returned by
  773.      `backup-buffer' and use that to set the mode bits of the file that
  774.      you write.  This is what `save-buffer' normally does.
  775.      Even though this is not a normal hook, you can use `add-hook' and
  776.      `remove-hook' to manipulate the list.  *Note Hooks::.
  777.  - Variable: local-write-file-hooks
  778.      This works just like `write-file-hooks', but it is intended to be
  779.      made local to particular buffers.  It's not a good idea to make
  780.      `write-file-hooks' local to a buffer--use this variable instead.
  781.      The variable is marked as a permanent local, so that changing the
  782.      major mode does not alter a buffer-local value.  This is
  783.      convenient for packages that read "file" contents in special ways,
  784.      and set up hooks to save the data in a corresponding way.
  785.  - Variable: write-contents-hooks
  786.      This works just like `write-file-hooks', but it is intended for
  787.      hooks that pertain to the contents of the file, as opposed to
  788.      hooks that pertain to where the file came from.  Such hooks are
  789.      usually set up by major modes, as buffer-local bindings for this
  790.      variable.
  791.      This variable automatically becomes buffer-local whenever it is
  792.      set; switching to a new major mode always resets this variable.
  793.      When you use `add-hooks' to add an element to this hook, you
  794.      should *not* specify a non-`nil' LOCAL argument, since this
  795.      variable is used *only* locally.
  796.  - Variable: after-save-hook
  797.      This normal hook runs after a buffer has been saved in its visited
  798.      file.
  799.  - Variable: file-precious-flag
  800.      If this variable is non-`nil', then `save-buffer' protects against
  801.      I/O errors while saving by writing the new file to a temporary
  802.      name instead of the name it is supposed to have, and then renaming
  803.      it to the intended name after it is clear there are no errors.
  804.      This procedure prevents problems such as a lack of disk space from
  805.      resulting in an invalid file.
  806.      As a side effect, backups are necessarily made by copying.  *Note
  807.      Rename or Copy::.  Yet, at the same time, saving a precious file
  808.      always breaks all hard links between the file you save and other
  809.      file names.
  810.      Some modes set this variable non-`nil' locally in particular
  811.      buffers.
  812.  - User Option: require-final-newline
  813.      This variable determines whether files may be written out that do
  814.      *not* end with a newline.  If the value of the variable is `t',
  815.      then `save-buffer' silently adds a newline at the end of the file
  816.      whenever the buffer being saved does not already end in one.  If
  817.      the value of the variable is non-`nil', but not `t', then
  818.      `save-buffer' asks the user whether to add a newline each time the
  819.      case arises.
  820.      If the value of the variable is `nil', then `save-buffer' doesn't
  821.      add newlines at all.  `nil' is the default value, but a few major
  822.      modes set it to `t' in particular buffers.
  823.  - Command: set-visited-file-name FILENAME &optional NO-QUERY
  824.      This function changes the visited file name of the current buffer
  825.      to FILENAME.  It also renames the buffer based on FILENAME,
  826.      appending a string like `<2>' if necessary to make a unique buffer
  827.      name.  It marks the buffer as *modified*,a since the contents do
  828.      not (as far as Emacs knows) match the actual file's contents.
  829.      If the specified file already exists, `set-visited-file-name' asks
  830.      for confirmation unless NO-QUERY is non-`nil'.
  831. File: elisp,  Node: Reading from Files,  Next: Writing to Files,  Prev: Saving Buffers,  Up: Files
  832. Reading from Files
  833. ==================
  834.    You can copy a file from the disk and insert it into a buffer using
  835. the `insert-file-contents' function.  Don't use the user-level command
  836. `insert-file' in a Lisp program, as that sets the mark.
  837.  - Function: insert-file-contents FILENAME &optional VISIT BEG END
  838.           REPLACE
  839.      This function inserts the contents of file FILENAME into the
  840.      current buffer after point.  It returns a list of the absolute
  841.      file name and the length of the data inserted.  An error is
  842.      signaled if FILENAME is not the name of a file that can be read.
  843.      The function `insert-file-contents' checks the file contents
  844.      against the defined file formats, and converts the file contents if
  845.      appropriate.  *Note Format Conversion::.  It also calls the
  846.      functions in the list `after-insert-file-functions'; see *Note
  847.      Saving Properties::.
  848.      If VISIT is non-`nil', this function additionally marks the buffer
  849.      as unmodified and sets up various fields in the buffer so that it
  850.      is visiting the file FILENAME: these include the buffer's visited
  851.      file name and its last save file modtime.  This feature is used by
  852.      `find-file-noselect' and you probably should not use it yourself.
  853.      If BEG and END are non-`nil', they should be integers specifying
  854.      the portion of the file to insert.  In this case, VISIT must be
  855.      `nil'.  For example,
  856.           (insert-file-contents filename nil 0 500)
  857.      inserts the first 500 characters of a file.
  858.      If the argument REPLACE is non-`nil', it means to replace the
  859.      contents of the buffer (actually, just the accessible portion)
  860.      with the contents of the file.  This is better than simply
  861.      deleting the buffer contents and inserting the whole file, because
  862.      (1) it preserves some marker positions and (2) it puts less data
  863.      in the undo list.
  864.    If you want to pass a file name to another process so that another
  865. program can read the file, use the function `file-local-copy'; see
  866. *Note Magic File Names::.
  867.